// pages/comment/comment.js
const app = getApp();
import {
  OrdersModel
} from "../../utils/models/orders/Orders-models";
var ordersModel = new OrdersModel;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: app.globalData.navbarHeight,
    titleName: '评价',
    navBgColor: "yellow",
    isLeft: true,
    maxCount: 9,
    t_length: 0,
    t_text: '',

    stars: [0, 1, 2, 3, 4],
    normalSrc: '/images/icon/sc.png', //未选中状态
    selectedSrc: '/images/icon/sc_s.png', //选中状态
    key: 0, //评分
    images: [],
    pics: [],
    detail:{}
    // upimgbtn: true,
  },

  selectServer: function (e) { //评分
    var key = e.currentTarget.dataset.key
    if (this.data.key == 1 && e.currentTarget.dataset.key == 1) { //当有一颗星的时候再次点击取消选中
      key = 0;
    }
    console.log("得" + key + "分")
    this.setData({
      key: key
    })
  },

  // 限制字数
  bindText: function (e) {
    var t_text = e.detail.value;
    var t_text_len = e.detail.value.length;
    this.setData({
      t_text: t_text,
      t_length: t_text_len
    }) 
  },

  // 点击上传图片

  upShopLogo: function () {
    var that = this;

    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImageShop('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImageShop('camera')
          }
        }
      }
    })
  },

  chooseWxImageShop: function (type) {
    var that = this;
    var picLen = this.data.pics.length;
    var maxCount = this.data.maxCount;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: maxCount - picLen,
      success: function (res) {
     
        /*上传多张（遍历数组，一次传一张）*/
        var pics = that.data.pics;
        if (pics.length >= maxCount) {
          wx.showToast({
            icon: 'none',
            title: '最多上传' + maxCount + '张'
          })
          return false
        }
        if (res.tempFilePaths.length <= (maxCount-picLen)) {
          var pics = that.data.pics;
          for (var index in res.tempFilePaths) {
            //暂无接口，先模拟图片上传
            pics.push(res.tempFilePaths[index]);
            that.upload_file('http://spare.com/index/order/uploadImg', res.tempFilePaths[index]); //上传图片操作
          }
          that.setData({
            pics: pics,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '您最多还能上传'+(maxCount-picLen)+'张'
          })
          return;
        }
      }
    })
  },

  upload_file: function (url, filePath) {
    var that = this;
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data',
        'token':wx.getStorageSync('token')
      }, // 设置请求的 header
   
      success: function (res) {
        if (res.statusCode == 200) {
          var result = JSON.parse(res.data);
          if (result.code > 0) {
            var images = that.data.images;
            images.push(result.data.filename)
            that.setData({
              images: images
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '上传失败'
            })
            return;
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: '上传失败'
          })
          return;
        }
      },

      fail: function (res) {
      }
    })

  },

  // 删除图片
  delimg: function(e) {
    var index = e.currentTarget.dataset.index;
    var pics = this.data.pics;
    pics.splice(index, 1);
    this.setData({
      pics: pics
    });
  },

  // 发表
  onPublish: function() {
    var t_text = this.data.t_text; // 描述
    var pics = this.data.pics; //上传图片组（临时路径）
    var key = this.data.key; //评分

    if(key<=0) {
      wx.showToast({
        icon: 'none',
        title: '评分最低1分'
      })
      return;
    }

    if(t_text.length<5) {
      wx.showToast({
        icon: 'none',
        title: '不能少于5字哦'
      })
      return;
    }
    var param = {
      'content': t_text,
      'score': key,
      'images': this.data.images,
      'oid':this.data.detail.id
    }
    var that = this;
    ordersModel.publish(param,function(res){
      console.log(res);
      if(res.code > 0){
        wx.showToast({
          title: '评价成功',
        })
        wx.navigateTo({
          url: './comment_success?oid=' + that.data.detail.id,
        })
      }
    })
    
  },
  getOrderDetail:function(id){
    var param = {'id':id}
    var that = this;
    ordersModel.getComment(param,function(res){
      that.setData({
        detail:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var id = options.id;
    this.getOrderDetail(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})